package doc.webapp;

import java.io.*;
import java.util.Properties;
import jetbrick.template.*;
import jetbrick.template.utils.IoUtils;
import org.apache.commons.lang3.StringUtils;

public class WebsiteApplication {
    private final JetEngine engine;

    public WebsiteApplication() {
        //@formatter:off
        String[] functions = new String[] {
            AppFunctions.class.getName()
        };
        //@formatter:on

        Properties config = new Properties();
        config.setProperty(JetConfig.IMPORT_FUNCTIONS, StringUtils.join(functions, ","));
        config.setProperty(JetConfig.COMPILE_DEBUG, "true");
        this.engine = JetEngine.create(config);

        System.out.println(engine.lookupResource("/file/./README.md"));
    }

    public void write(String layoutFile, String file, String title, String description) throws FileNotFoundException {
        JetContext context = new JetContext();
        context.put("title", title);
        context.put("url", file + ".html");
        context.put("markdownFile", "/doc-template/" + file + ".md");
        context.put("lastDate", "2014-03-10");
        context.put("lastVersion", "1.2.3");
        context.put("description", description);
        
        JetTemplate template = engine.getTemplate(layoutFile);
        OutputStream out = new FileOutputStream(new File("./WebRoot/"+file+".html"));//
        template.render(context, out);
        IoUtils.closeQuietly(out);
    }

    public static void main(String[] args) throws Throwable {
        WebsiteApplication app = new WebsiteApplication();
        app.write("/doc-template/layout.jetx", "overview", "简介", "jetbrick-template 是一个新一代 Java 模板引擎");
        app.write("/doc-template/layout.jetx", "download", "下载", "");
        app.write("/doc-template/layout.jetx", "history", "历史版本下载", "");
        app.write("/doc-template/layout.jetx", "userguide", "开发指南", "");
        app.write("/doc-template/layout.jetx", "config", "配置指南", "");
        app.write("/doc-template/layout.jetx", "syntax", "语法指南", "");
        app.write("/doc-template/layout.jetx", "integrate", "Web 框架集成", "");
        app.write("/doc-template/layout.jetx", "faq", "FAQ", "");
        app.write("/doc-template/layout.jetx", "faq-compile", "将模板编译成 Java Class 有什么好处？", "");
        app.write("/doc-template/layout.jetx", "faq-define", "为什么需要 #define 声明变量类型？", "");
        app.write("/doc-template/layout.jetx", "faq-error", "常见错误分析", "");
        app.write("/doc-template/layout.jetx", "faq-tag", "如何自定义 Tag", "");
        app.write("/doc-template/layout.jetx", "faq-layout", "如何实现 layout 功能", "");
        app.write("/doc-template/layout.jetx", "faq-include", "如何嵌入子模板", "");
        app.write("/doc-template/layout.jetx", "faq-spring", "Spring 中的集成方法", "");
        app.write("/doc-template/layout.jetx", "faq-autoscan", "让自动扫描发现用户自定义的扩展方法/函数/标签 Class", "");
        app.write("/doc-template/layout.jetx", "about", "关于", "");
        app.write("/doc-template/layout.jetx", "coding-guide", "JAVA 编码规范", "");
        app.write("/doc-template/layout.jetx", "test", "Test", "");

        //app.write("/doc-template/book_pdf.jetx", "", "", new File("./WebRoot/book_pdf.html"));
        System.out.println("文档创建成功！");
    }
}
